package com.example.myprojectoffeixue;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//网关列表的适配器
public class GridViewAdapter extends BaseAdapter {
    private Context context;
    int[] mainInterfaceImageIDs;
    String[] mainInterfaceStr;
    Map<Integer,String> data;

    public GridViewAdapter(Context context,int[] mainInterfaceImageIDs,String[] mainInterfaceStr) {
        this.context = context;
        data=new HashMap<Integer,String>();
        for(int i=0;i<6;i++){
            data.put(i,"0");
        }
        this.mainInterfaceImageIDs =mainInterfaceImageIDs;
        this.mainInterfaceStr = mainInterfaceStr;
    }

    @Override
    public int getCount() {
        return mainInterfaceImageIDs.length;
    }

    @Override
    public Object getItem(int position) {
        return null;

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_h2, null);
            viewHolder = new ViewHolder();
            viewHolder.iv = (ImageView) convertView.findViewById(R.id.itemImage);
            viewHolder.tv = (TextView) convertView.findViewById(R.id.itemText);
            viewHolder.tv2 = (TextView) convertView.findViewById(R.id.add);
            viewHolder.tv3 = (TextView) convertView.findViewById(R.id.jian);
            viewHolder.tv4 = (TextView) convertView.findViewById(R.id.data);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.iv.setImageResource(mainInterfaceImageIDs[position]);
        viewHolder.tv.setText(mainInterfaceStr[position]);
        viewHolder.tv4.setText(data.get(position));
        viewHolder.tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(data.get(position).equals("6")){
                    data.put(position,"6");
                }else {
                    int value=Integer.parseInt(data.get(position));
                    value++;
                    data.put(position,value+"");

                }

                GridViewAdapter.this.notifyDataSetChanged();
            }
        });

        viewHolder.tv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(data.get(position).equals("0")){
                    data.put(position,"0");
                }else {
                    int value=Integer.parseInt(data.get(position));
                    value--;
                    data.put(position,value+"");

                }

                GridViewAdapter.this.notifyDataSetChanged();

            }
        });






        return convertView;


    }


    class ViewHolder {
        TextView tv;
        ImageView iv;
        TextView tv2;
        TextView tv3;
        TextView tv4;
    }


}